﻿@{
    ViewBag.Title = "工资公式";
}
@section header{

}
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <input id="zTreesso" name="zTreesso" type="text" style="width:100%;height:25px;" placeholder="请输入关键字" />
        <ul id="itemTree" class="ztree"></ul>
    </div>
    <div class="ui-layout-center">
        <div class="topPanel">
            <div class="toolbar">
                <div class="btn-group">
                    <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
                </div>
                <div class="btn-group">
                    <a id="NF-add" authorize="yes" class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>添加工资列</a>
                    <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                    <ul class="dropdown-menu pull-right">
                        <li><a id="NF-gzl" authorize="yes" onclick="btn_gzl()" href="javascript:void()">工资册管理</a></li>
                    </ul>
                </div>
                <div class="operate">
                    <ul class="nav nav-pills">
                        <li class="first">已选中<span>1</span>项</li>
                        <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改</a></li>
                        <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除</a></li>
                        <li><a id="NF-Details" authorize="yes" onclick="btn_details()"><i class="fa fa-search-plus"></i>查看</a></li>
                    </ul>
                    <a href="javascript:;" class="close"></a>
                </div>
                <script>$('.toolbar').authorizeButton()</script>
            </div>
            <div class="search">
                <table>
                    <tr>
                        <td>
                            <div class="input-group">
                                <input id="txt_keyword" type="text" class="form-control" placeholder="名称/编号" style="width: 200px;">
                                <span class="input-group-btn">
                                    <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                                </span>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="gridPanel">
            <table id="gridList"></table>
            <div id="gridPager"></div>
        </div>
    </div>
</div>

@section scripts{
    <script>
        var baseUrl = "/reward/SalaryColumn/";
        var treeNodeId = "";
        var $gridList;
        $(function () {
            $('#layout').layout();
            $gridList = $("#gridList");
            treeView();
            gridList(); 
        })
        function treeView() {
            var setting = {
                view: {
                    dblClickExpand: false,
                    showLine: true,
                    selectedMulti: false
                },
                data: {
                    key: {
                        name: "Name",
                    },
                    simpleData: {
                        enable: true,
                        idKey: "ID",
                        pIdKey: "ParentID",
                        rootPId: ""
                    }
                },
                callback: {
                    // 单击事件
                    onClick: function zTreeOnClick(event, treeId, treeNode) {
                        //console.log(treeNode)
                        treeNodeId = treeNode.ID;
                        $gridList.jqGrid('setGridParam', {
                            postData: { keyword: $("#txt_keyword").val(), Treeword: treeNodeId },
                        }).trigger('reloadGrid');
                    }
                }
            };
            $.ajax({
                url: baseUrl + "GetSalaryColumnJson",
                type: "post",
                dataType: "json",
                success: function (data) {
                    var zTreeObj = $.fn.zTree.init($("#itemTree"), setting, data);
                    fuzzySearch('itemTree', '#zTreesso', null, true); //初始化模糊搜索方法
                }
            });
        }
        function gridList() {
            $gridList.dataGrid({
                url: baseUrl + "GetBranchGridJson",
                datatype: "json",
                height: $(window).height() - 128,
                mtype: "POST",
                colModel: [
                    { label: "主键", name: "ID", hidden: true, key: true },
                    {
                        label: '排序', name: 'OrderBy', width: 100, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return "<input type='text' name='OrderBy[" + keyId + "]' autocomplete='off' style='width: 50px; height: 20px;' onchange='update_field(\"" + keyId + "\",this.value)' value=" + cellvalue + " />";
                        }
                    },
                    { label: '名称', name: 'Name', align: 'left' },
                    {
                        label: "计算类型", name: "SalaryType", align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            // 1公式调用，2直接输入，3固定工资，4其它导入
                            if (cellvalue == 1) {
                                return "公式调用";
                            } else if (cellvalue == 2) {
                                return "直接输入";
                            } else if (cellvalue == 3) {
                                return "固定工资";
                            } else if (cellvalue == 4) {
                                return "其它导入";
                            } else {
                                return "其它";
                            }
                        }
                    },
                    { label: '公式', name: 'OperateExpression', width: 350, align: 'left' },
                    {
                        label: '添加时间', name: 'AddTime', width: 90, align: 'left',
                        formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                    }
                ],
                rowNum: 20,
                rowList: [20, 50, 100],
                pager: '#gridPager',
                sortname: 'OrderBy',
                sortorder: "asc",
                viewrecords: true,
                autowidth: true,
                multiselect: true,
                loadComplete: function () {
                    $('.ui-jqgrid-bdiv').scrollTop(0);
                    setTimeout(function () { btn_tc(); });
                }
            });
            $("#btn_search").click(function () {
                $gridList.jqGrid('setGridParam', {
                    postData: { keyword: $("#txt_keyword").val() },
                }).trigger('reloadGrid');
            });
        }
        function btn_add() {
            if (treeNodeId == "") {
                $.modalMsg("请选中左边的工资条", "error");
                return;
            }
            $.modalOpen({
                id: "Form",
                title: "添加工资列",
                url: baseUrl + "Form?IfType=0&ParentID=" + treeNodeId,
                width: "700px",
                height: "440px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_edit() {
            var s = $("#gridList").jqGrid('getGridParam', 'selarrrow');
            if (s.length > 0) {
                var keyValue = s[0];
                $.modalOpen({
                    id: "Form",
                    title: "修改工资列",
                    url: baseUrl + "Form?IfType=0&keyValue=" + keyValue,
                    width: "700px",
                    height: "440px",
                    callBack: function (iframeId) {
                        top.frames[iframeId].submitForm();
                    }
                });
            } else {
                $.modalMsg("请选择一条数据", "error");
            }
        }
        function btn_delete() {
            var s = $("#gridList").jqGrid('getGridParam', 'selarrrow');
            if (s.length > 0) {
                console.log(s.toString())
                $.deleteForm({
                    url: baseUrl + "DeleteForm",
                    param: { keyValue: s.toString() },
                    success: function () {
                        $.currentWindow().$("#gridList").resetSelection();
                        $.currentWindow().$("#gridList").trigger("reloadGrid");
                    }
                })
            } else {
                $.modalMsg("请选择一条数据", "error");
            }
        }
        function btn_details() {
            var keyValue = $("#gridList").jqGridRowValue().ID;
            $.modalOpen({
                id: "Details",
                title: "查看",
                url: baseUrl + "Details?IfType=0&keyValue=" + keyValue,
                width: "700px",
                height: "490px",
                btn: null,
            });
        }
        function update_field(keyId, cellvalue) {
            $.ajax({
                url: baseUrl + "UpdateOrderBy?keyId=" + keyId + "&value=" + cellvalue,
                data: {},
                dataType: "json",
                type: "POST",
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
        /*工资册*/
        function btn_gzl() {
            $.modalOpen({
                id: "gzlType",
                title: "工资册",
                url: "/reward/SalaryColumn/GzlList",
                width: "800px",
                height: "550px",
                btn: null,
            });
        }
    </script>
}